Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : SPB Private Cloud8 * File : app/api/folders/[id]/download/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/spb-cloud11 * Project Website : https://cloud.spboucher.ai12 * Created : 2026-02-1813 * Last Modified : 2026-09-0714 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { NextRequest, NextResponse } from "next/server";43import { prisma } from "@/lib/prisma";44import { loadFileData } from "@/lib/storage";45import JSZip from "jszip";4647async function addFolderToZip(zip: JSZip, folderId: string, prefix: string) {48 const files = await prisma.file.findMany({ where: { folderId, deletedAt: null } });49 for (const file of files) {50 try {51 const data = await loadFileData(file.storagePath, file.isEncrypted);52 zip.file(prefix + file.name, data); // déchiffré dans l'archive53 } catch {54 // fichier manquant/illisible : ignoré55 }56 }57 const subfolders = await prisma.folder.findMany({ where: { parentId: folderId } });58 for (const sub of subfolders) {59 await addFolderToZip(zip, sub.id, `${prefix}${sub.name}/`);60 }61}6263export async function GET(_request: NextRequest, { params }: { params: { id: string } }) {64 const folder = await prisma.folder.findUnique({ where: { id: params.id } });65 if (!folder) return NextResponse.json({ error: "Not found" }, { status: 404 });6667 const zip = new JSZip();68 await addFolderToZip(zip, folder.id, "");69 const buffer = await zip.generateAsync({ type: "nodebuffer" });7071 return new NextResponse(new Uint8Array(buffer), {72 headers: { "Content-Type": "application/zip", "Content-Disposition": `attachment; filename="${folder.name}.zip"` },73 });74}75